package com.kinghunt.misp.service.impl.devicex;

import com.bygoodtech.kings.base.utils.ConvertUtil;
import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.kinghunt.misp.common.dto.devicex.DeviceDTO;
import com.kinghunt.misp.common.dto.devicex.DeviceInspectionDTO;
import com.kinghunt.misp.common.enums.devicex.DeviceTypeEnum;
import com.kinghunt.misp.common.param.devicex.DeviceInspectionParam;
import com.kinghunt.misp.dal.dao.devicex.DeviceInspectionDAO;
import com.kinghunt.misp.dal.model.devicex.DeviceInspectionDO;
import com.kinghunt.misp.service.api.devicex.DeviceInspectionService;
import com.kinghunt.misp.service.api.devicex.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@Slf4j
@Service
@Transactional
public class DeviceInspectionServiceImpl implements DeviceInspectionService {

    @Autowired(required = false)
    private DeviceInspectionDAO deviceInspectionDAO;
    @Autowired(required = false)
    private DeviceService deviceService;

    @Override
    public ApiResult<PageInfo<DeviceInspectionDTO>> getDeviceInspectionList(DeviceInspectionParam deviceInspectionParam) {
        int offset = (deviceInspectionParam.getPageNum() - 1) * deviceInspectionParam.getPageSize();
        DeviceInspectionDO deviceInspectionDO = ConvertUtil.convert(deviceInspectionParam, DeviceInspectionDO.class);
        int count = deviceInspectionDAO.selectDeviceInspectionListCount(deviceInspectionDO);
        PageInfo<DeviceInspectionDTO> pageInfo = new PageInfo<>();
        pageInfo.setTotal(count);
        pageInfo.setPageNum(deviceInspectionParam.getPageNum());
        pageInfo.setPageSize(deviceInspectionParam.getPageSize());
        List<DeviceInspectionDO> deviceInspectionDOList = deviceInspectionDAO.selectDeviceInspectionList(deviceInspectionDO, offset, deviceInspectionParam.getPageSize());
        List<DeviceInspectionDTO> dto =  ConvertUtil.convert(deviceInspectionDOList, DeviceInspectionDTO.class);
        pageInfo.setData(dto);
        return ApiResult.ok(pageInfo);
    }

    @Override
    public ApiResult<DeviceInspectionDTO> getDeviceInspectionDetail(Long inspectionId) {
        DeviceInspectionDO deviceInspectionDO = deviceInspectionDAO.selectByPrimaryKey(inspectionId);
        DeviceInspectionDTO deviceInspectionDTO = ConvertUtil.convert(deviceInspectionDO, DeviceInspectionDTO.class);
        return ApiResult.ok(deviceInspectionDTO);
    }

    @Override
    public ApiResult addDeviceInspection(DeviceInspectionParam deviceInspectionParam) {
        ApiResult<DeviceDTO> apiResult= deviceService.getDeviceDetail(deviceInspectionParam.getDeviceId());
        if(!apiResult.isSuccess() || null == apiResult.getData() ){
            return ApiResult.failOfMessage("您申请巡检的设备不存在！");
        }
        DeviceInspectionDO deviceInspectionDO = ConvertUtil.convert(deviceInspectionParam, DeviceInspectionDO.class);
        // 保存地址到数据库
        deviceInspectionDAO.insert(deviceInspectionDO);
        return ApiResult.ok();
    }
}
